'use strict';

// 引入axios
import axios from 'axios';

// axios.defaults.withCredentials = true;

/**
 * 创建axios对象实例
 * @type {AxiosInstance}
 */
let instance = axios.create({
    // baseURL: 'https://some-domain.com/api/',
    // timeout: 1000,
    // headers: {'X-Custom-Header': 'foobar'}
});

/**
 * 参数信息
 * @type {{RequestMethod: {DELETE: string, POST: string, GET: string, PUT: string}, BASE_HTTP: string, HEADER: {}, ResponseType: {BLOB: string, NULL: string, JSON: string, TEXT: string}}}
 */
const param = {
    /**
     * 请求方式
     */
    RequestMethod : {
        POST: 'post',
        GET: 'get',
        PUT: 'put',
        DELETE: 'delete',
    },
    /**
     * 响应类型
     */
    ResponseType : {
        NULL: '',
        JSON: 'json',
        TEXT: 'text',
        BLOB: 'blob',
    },
    /**
     * 默认请求地址
     */
    BASE_HTTP : "http://127.0.0.1:8888",
    // BASE_HTTP : "http://47.96.131.22:8888",
    // BASE_HTTP : "http://116.62.19.197:8083/LearningPlatform/",
    // BASE_HTTP : "http://localhost:8080/LearningPlatform",
    /**
     * 默认请求头参数
     */
    HEADER : {
        // 'Content-Type':'application/json;charset=UTF-8',
        // 'Access-Control-Allow-Origin': 'http://116.62.19.197',
        // 'Accept':'text/Html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
    },
};

export default {
    /**
     * 获取参数
     * @return {{RequestMethod: {DELETE: string, POST: string, GET: string, PUT: string}, BASE_HTTP: string, HEADER: {}, ResponseType: {BLOB: string, NULL: string, JSON: string, TEXT: string}}}
     */
    getParam() {
        return param;
    },
    /**
     * ajaxPost请求
     * @param url
     * @param params
     * @param success
     * @param type
     */
    ajaxPost(url, params, success, responseType = param.ResponseType.JSON) {
        this.ajax(url, params, success, param.RequestMethod.POST, responseType);
    },
    /**
     * ajaxGet请求
     * @param url
     * @param params
     * @param success
     * @param responseType
     */
    ajaxGet(url, params, success, responseType = param.ResponseType.JSON) {
        this.ajax(url, params, success, param.RequestMethod.GET, responseType);
    },
    /**
     * ajax请求
     * @param url
     * @param params
     * @param success
     * @param method
     * @param responseType
     */
    ajax(url, params, success, method, responseType = param.ResponseType.JSON) {
        instance({
            method,
            url: param.BASE_HTTP + url,
            headers: param.HEADER,
            params,
            withCredentials: true,
            responseType
        }).then((response) => {
            // 验证是否成功返回数据
            success(response.data);
            // if(response.data.success) {
            //     success(response.data);
            // }else {
            //     message.error(that, response.data.message)
            // }
        }).catch((error) => {
            if (error.response) {
                // 请求已发出，但服务器响应的状态码不在 2xx 范围内
                console.log(error.response.data);
                console.log(error.response.status);
                console.log(error.response.headers);
            } else {
                console.log('Error', error.message);
            }
            console.log(error.config);
        })
    },
}
